package com.ruoyi.alarm.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.alarm.domain.AjtAlarmData;
import com.ruoyi.alarm.service.IAjtAlarmDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预警信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/alarm/alarmData")
public class AjtAlarmDataController extends BaseController
{
    @Autowired
    private IAjtAlarmDataService ajtAlarmDataService;

    /**
     * 查询预警信息列表
     */
    // @PreAuthorize("@ss.hasPermi('alarm:alarmData:list')")
    @GetMapping("/list")
    public TableDataInfo list(AjtAlarmData ajtAlarmData)
    {
        startPage();
        List<AjtAlarmData> list = ajtAlarmDataService.selectAjtAlarmDataList(ajtAlarmData);
        return getDataTable(list);
    }

    /**
     * 导出预警信息列表
     */
    //@PreAuthorize("@ss.hasPermi('alarm:alarmData:export')")
    @Log(title = "预警信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AjtAlarmData ajtAlarmData)
    {
        List<AjtAlarmData> list = ajtAlarmDataService.selectAjtAlarmDataList(ajtAlarmData);
        ExcelUtil<AjtAlarmData> util = new ExcelUtil<AjtAlarmData>(AjtAlarmData.class);
        util.exportExcel(response, list, "预警信息数据");
    }

    /**
     * 获取预警信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('alarm:alarmData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ajtAlarmDataService.selectAjtAlarmDataById(id));
    }

    /**
     * 新增预警信息
     */
    //@PreAuthorize("@ss.hasPermi('alarm:alarmData:add')")
    @Log(title = "预警信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AjtAlarmData ajtAlarmData)
    {
        return toAjax(ajtAlarmDataService.insertAjtAlarmData(ajtAlarmData));
    }

    /**
     * 修改预警信息
     */
    //@PreAuthorize("@ss.hasPermi('alarm:alarmData:edit')")
    @Log(title = "预警信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AjtAlarmData ajtAlarmData)
    {
        return toAjax(ajtAlarmDataService.updateAjtAlarmData(ajtAlarmData));
    }

    /**
     * 删除预警信息
     */
    //@PreAuthorize("@ss.hasPermi('alarm:alarmData:remove')")
    @Log(title = "预警信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ajtAlarmDataService.deleteAjtAlarmDataByIds(ids));
    }

    /**
     * 统计里程数接口
     * @param limit 统计的记录数
     * @return 统计结果
     */
    @GetMapping("/statisticsMileage")
    public List<Map<String, Object>> statisticsMileage(@RequestParam("limit") int limit) {
        return ajtAlarmDataService.statisticsMileage(limit);
    }

    /**
     * 统计指定天数内报警次数最多的前10个车辆
     * @param days 指定天数
     * @return 报警数据列表
     */
    @GetMapping("/getTop10AlarmVehiclesInLastDays")
    public AjaxResult getTop10AlarmVehiclesInLastDays(@RequestParam("days") int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -days);
        Date startTime = calendar.getTime();
        List<Map<String, Object>> result = ajtAlarmDataService.getTop10AlarmVehiclesInLastDays(startTime);
        return success(result);
    }
}
